package com.mingtu.dzpatrol.utils;

/**
 * @ClassName :Activity
 * @Author : wkk
 * @Date : 2020/4/24 16:59:27
 * @Email : 1281573890@qq.com
 * @Eescription :工具类
 */

import android.app.Activity;

import com.blankj.utilcode.util.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

import static com.blankj.utilcode.util.ActivityUtils.isActivityAlive;

public class ActivityUtil {
    private static List<Activity> activityList = new ArrayList<>();
    private static ActivityUtil instance;

    /**
     * 单例模式中获取唯一的ExitApplication实例
     */

    public static synchronized ActivityUtil getInstance() {
        if (null == instance) {
            instance = new ActivityUtil();
        }
        return instance;
    }

    /**
     * 获取所有activity
     */
    public static List<Activity> getActivityList() {
        return activityList;
    }

    /**
     * 添加Activity到容器中
     */
    public void addActivity(Activity activity) {
        if (activityList == null)
            activityList = new ArrayList<>();
        activityList.add(activity);
    }

    /**
     * 获取顶部activity
     */
    public static Activity getTopActivity() {
//        List<Activity> activityList = getActivityList();
//        for (Activity activity : activityList) {
//            if (!isActivityAlive(activity)) {
//                continue;
//            }
//            return activity;
//        }
        return ActivityUtils.getTopActivity();
    }

    /**
     * 移除Activity
     */
    public static void removeActivity(Activity activity) {
        if (activityList == null) return;
        if (activityList != null) {
            activity.finish();
//            activity.overridePendingTransition(0, 0);
            activityList.remove(activity);
            activity = null;
        }
    }

    /**
     * 清除Activity
     */

    public static void clearActivity() {
        if (activityList != null) {
            for (Activity activity : activityList) {
                if (!activity.isFinishing()) {
                    activity.finish();
                }
            }
            activityList.clear();
        }
    }

    /**
     * 结束所有的Activity，退出应用
     */
    public static void removeAllActivity() {
        if (activityList != null && activityList.size() > 0) {
            for (Activity activity : activityList) {
                activity.finish();
            }
        }
    }

    /**
     * 结束所有的Activity，退出应用
     */
    public static void exitSystem() {
        if (activityList != null) {
            for (Activity activity : activityList) {
                if (activity != null)
                    activity.finish();
            }
            activityList.clear();
        }
        // 退出进程
        android.os.Process.killProcess((android.os.Process.myPid()));
        System.exit(0);
    }

}
